package com.gnerv.pabu.basic.core.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.pabu.basic.core.service.IKafkaListenerService;
import com.gnerv.pabu.basic.core.service.IPermissionService;
import com.gnerv.pabu.basic.core.entity.Result;
import com.gnerv.pabu.basic.core.dto.PermissionDTO;
import com.gnerv.pabu.basic.core.dto.PermissionSaveDTO;
import com.gnerv.pabu.basic.core.dto.PermissionSearchDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 基础平台-权限表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-10-10
 */
@Api(tags = "权限管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/manage/permission")
public class PermissionController {

    @Resource
    private IPermissionService permissionService;

    @ApiOperation(value = "保存 权限")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result savePermission(@RequestBody PermissionSaveDTO permissionSaveDTO) {
        permissionService.savePermission(permissionSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "新建 权限")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createPermission(@RequestBody PermissionSaveDTO permissionSaveDTO) {
        permissionService.createPermission(permissionSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "删除 权限")
    @RequestMapping(value = "/delete/{permissionId}", method = RequestMethod.GET)
    public Result deleteByPermissionId(@PathVariable(value = "permissionId") String permissionId) {
        permissionService.deleteByPermissionId(permissionId);
        return Result.response();
    }

    @ApiOperation(value = "修改 权限")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updatePermission(@RequestBody PermissionSaveDTO permissionSaveDTO) {
        permissionService.updatePermission(permissionSaveDTO);
        return Result.response();
    }

    @ApiOperation(value = "修改 权限状态")
    @RequestMapping(value = "/{permissionId}/status/{status}", method = RequestMethod.GET)
    public Result updatePermissionStatus(@PathVariable(value = "permissionId") String permissionId,
                                         @PathVariable(value = "status") Integer status) {
        permissionService.updatePermissionStatus(permissionId, status);
        return Result.response();
    }

    @ApiOperation(value = "查询权限 根据权限逻辑主键")
    @RequestMapping(value = "/select/{permissionId}", method = RequestMethod.GET)
    public Result selectPermission(@PathVariable(value = "permissionId") String permissionId) {
        PermissionDTO permission = permissionService.selectPermission(permissionId);
        return Result.response().data("permission", permission);
    }

    @ApiOperation(value = "查询权限列表 带分页 支持模糊查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listPermission(PermissionSearchDTO permission,
                                     @RequestParam(value = "current", defaultValue = "1") Long current,
                                     @RequestParam(value = "size", defaultValue = "300") Long size) {
        Page<PermissionDTO> permissions = permissionService.listPermission(permission, current, size);
        PermissionDTO permissionDTO = permissions.getRecords().get(0);
        return Result.response().data("permissions", permissions);
    }

}
